package com.edie.customview.data.net.api;

import android.support.annotation.NonNull;

import com.edie.customview.data.net.HttpLoggingInterceptor;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * edie create on 2018/7/10
 */
public class NetClient1 {
    public static final String API_BASE_URL_ON_LINE = "https://qpds.007vin.com/";//正式环境
    public static final String API_BASE_URL = "http://union.007vin.com/";//测试环境
    private static NetClient1 sInstance;
    private final Retrofit mRetrofit;

    private NetClient1() {
        OkHttpClient client = getOkHttpClient();
        mRetrofit = new Retrofit.Builder()
                .client(client)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .baseUrl(API_BASE_URL)
                .build();
    }

    @NonNull
    private OkHttpClient getOkHttpClient() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build();
    }

    public static NetClient1 getInstance() {
        if (sInstance == null) {
            synchronized (NetClient1.class) {
                if (sInstance == null) {
                    sInstance = new NetClient1();
                }
            }
        }
        return sInstance;
    }


    public UserApi createUserApi() {
        return createApi(UserApi.class);
    }

    public <T> T createApi(Class<T> t) {
        return mRetrofit.create(t);
    }
}
